<?php
namespace app\controller;

use app\BaseController;
use \think\facade\Request;
use \think\facade\Cache;
use \think\facade\Db;
use \think\facade\Log;
class System extends BaseController
{
  public function login()
  {
    $param = Request::param();
    $data = Db::table('store_info')
        ->where('uname', $param['uname'])
        ->where('upwd', $param['upwd'])
        ->find();
    if($data){
        $this->returnJson(200,'登录成功',$data);
    }else{
        $this->returnJson(400,'账号密码输入错误',[]);
    }
  }
  // 新增编辑岗位
  public function addEditPost()
  {
	  $param = Request::post();
	  unset($param['user_name']);
	  if(!isset($param['id'])){
		  $param['add_time'] = date("Y-m-d H:i:s");
		  $param['update_time'] = date("Y-m-d H:i:s");
		  Db::table('post_job')->insert($param);
		  $this->returnJson(200,'添加成功',[]);
	  }else{
		  $param['update_time'] = date("Y-m-d H:i:s");
		   Db::table('post_job')
		      ->where('id', $param['id'])
		      ->update($param);
		  $this->returnJson(200,'修改成功',[]);
	  }
  }
  // 更新权限菜单
  public function updateAuth()
  {
	   $param = Request::post();
	   $param['update_time'] = date("Y-m-d H:i:s");
	    Db::table('post_job')
	       ->where('id', $param['id'])
	       ->update([
			   'update_time'=>date("Y-m-d H:i:s"),
			   'checkNode'=>$param['checkNode']
		   ]);
	   $this->returnJson(200,'修改成功',[]);
  }
  // 获取岗位信息
  public function postJobList()
  {
        $param = Request::param();
		$map=[];
		if(isset($param['status'])){
			$myArray = array('status','=',$param['status']);
			array_push($map,$myArray);
		}
        
        if(isset($param['page'])){
            $page=$param['page'];
            $pageSize = $param['pageSize'];
            $page=($page-1)*$pageSize;
            $list = Db::name('post_job')
                ->limit($page,$pageSize)
                ->order('id','desc')
                ->where($map)
                ->whereNull('delete_time')
                ->select();
        }else{
            $list = Db::name('post_job')
                ->order('id','desc')
                ->where($map)
                ->whereNull('delete_time')
                ->select();
        }

        $count = Db::name('post_job')
                ->where($map)
                ->whereNull('delete_time')
                ->count();

        $dd = [
            'result'=>$list,
            'count'=>$count
        ];
        $this->returnJson(200,'查询成功',$dd);
  }
  // 获取菜单数据
  public function getMenuList()
  {
	  $param = Request::param();
	  $result = Db::table('menu_sys')
        ->field('id,parent_id,title,url,icon,sort,url,update_time,is_show')
	    ->where('parent_id','0')
        ->order('sort desc,id desc')
        ->whereNull('delete_time')
	    ->select()
		->all();
	  
	  
	  foreach($result as &$ele){
		  $ele['children'] = Db::table('menu_sys')
                            ->field('id,parent_id,title,url,icon,sort,url,update_time,is_show')
							->where('parent_id',$ele['id'])
							->order('sort desc,id desc')
							->whereNull('delete_time')
							->select();
	  }
	  
	  $data = [];
      if(isset($param['id'])){
		  $data['checkNode'] = Db::table('post_job')
							->where('id',$param['id'])
							->find()['checkNode'];
	  }
	  
	  $data['result'] = $result;
	  
	  $this->returnJson(200,'查询成功',$data);
  }
  // 修改菜单数据
  public function updateMenu()
  {
	  $param = Request::post();
      $dd = [
        'icon'=>$param['icon'],
        'is_show'=>$param['is_show'],
        'sort'=>$param['sort'],
        'title'=>$param['title'],
        'update_time'=>date("Y-m-d H:i:s"),
        'parent_id'=>$param['parent_id'],
      ];

	  $param['update_time'] = date("Y-m-d H:i:s");
	   Db::table('menu_sys')
	      ->where('id', $param['id'])
	      ->update($dd);
	  $this->returnJson(200,'修改成功',[]);
  }
  // 获取岗位菜单
  public function getMenuByJobId()
  {
	  $param = Request::param();
	  $res = Db::table('post_job')
			->where('id',$param['job_id'])
            ->whereNull('delete_time')
			->find();
	  
	  $result = Db::table('menu_sys')
					->whereIn('id',$res['checkNode'])
                    ->order('sort desc,id desc')
                    ->whereNull('delete_time')
					->select();
	  $this->returnJson(200,'查询成功',$result);			
  }
  //新增编辑用户信息
  public function addEditUserAdmin()
  {
      $param = Request::param();
	  
      //存在不能添加
		unset($param['user_name']);
          //不存在 判断有没有uid
          // 有uid编辑 没有添加
          if(!isset($param['id'])){
              $bool = Db::table('yh_company')
                  ->where('uname', $param['uname'])
				  ->whereNull('delete_time')
                  ->find();
              if($bool){
                  $this->returnJson(400,'该用户已经存在',[]);
              }
              $param['upwd']=  md5($param['upwd']);
              $param['add_time'] = date("Y-m-d H:i:s");
              $param['update_time'] = date("Y-m-d H:i:s");
              Db::table('yh_company')->insert($param);
              $this->returnJson(200,'添加成功',[]);
          }else{
              unset($param['upwd']);
              $param['update_time'] = date("Y-m-d H:i:s");
               Db::table('yh_company')
                  ->where('id', $param['id'])
                  ->update($param);
              $this->returnJson(200,'修改成功',[]);
          }

  }
  //获取用户列表
  public function userList()
  {
        $param = Request::param();
        
        //分页
        if(isset($param['page'])){
            $page=$param['page'];
            $pageSize = $param['pageSize'];
            $page=($page-1)*$pageSize;
            $list = Db::table('yh_company')
                ->limit($page,$pageSize)
                ->order('id','desc')
                ->whereNull('delete_time')
                ->select()
                ->all();
        }else{
            $list = Db::table('yh_company')
                ->order('id','desc')
                ->whereNull('delete_time')
                ->select()
                ->all();
        }

        foreach($list as &$ele){
            $ele['post_job_name'] = Db::table('post_job')
                            ->where('id',$ele['job'])
                            ->value('post_name');
        }

        $count = Db::table('yh_company')
                ->whereNull('delete_time')
                ->count();

        $dd = [
            'result'=>$list,
            'count'=>$count
        ];
        $this->returnJson(200,'查询成功',$dd);
  }
  //修改用户密码
  public function editUserUpwdAdmin()
  {
      $param = Request::param();
    //   $upwd =  password_hash($param['upwd'], PASSWORD_DEFAULT);
    $upwd = md5($param['upwd']);
      $count = Db::table('yh_company')
          ->where('id', $param['id'])
          ->update([
              'upwd' => $upwd,
          ]);
      if($count != 0){
          $this->returnJson(200,'更新成功');
      }else{
          $this->returnJson(400,'更新失败请稍后再试');
      }


  }
  //删除用户
  public function delUser()
  {
	  $param = Request::param();
	  Db::name('user_info')
			->where('cid',$param['cid'])
			->where('uid',$param['uid'])
			->useSoftDelete('delete_time',time())
			->delete();
	  $this->returnJson(200,'操作成功',[]);
  }
  

}